14. CODE: Write FindNeighbor
Write FindNeighbor
L4 Before FindNeighbor
In this exercise, modify route_model.h
and route_model.cpp
to add a RouteModel::Node
method FindNeighbor
. The goal of FindNeighbor
is to return a pointer to the closest unvisited node from a vector of node indices, where the distance is measured to the current node (this
). This method will be used later to help find all of the possible next steps in the A* search.
## To complete this exercise:
- Add a
FindNeighbor
declaration to theRouteModel::Node
class inroute_model.h
. This method will only be used later in anotherRouteModel::Node
method to find the closest node in eachRoad
containing the current node, soFindNeighbor
can be a private method.FindNeighbor
should accept avector<int> node_indices
argument and return a pointer to a node:RouteModel::Node*
type. - In
route_model.cpp
define an emptyFindNeighbor
method. At this step, compile the code usingmake
to check that your method declaration and empty method definiton have matching signatures. - Within the
FindNeighbor
method, loop through thenode_indices
vector to find the closest unvisited node. To do this, start with a pointerNode *closest_node = nullptr
, and then updateclosest_node
as you find closer nodes in the loop. The following will be useful:- For each index in the loop, you can retrieve the
Node
object withparent_model->SNodes()[index]
. - For each retrieved
Node
in the loop, you should check that the node has not been visted (!node.visited
) and that the distance tothis
is nonzero. In other words, you want the closest unvisted node that is not the current node. - The
RouteModel::Node::distance
method can be used to find the distance between two nodes.
- For each index in the loop, you can retrieve the
## Pseudocode:
The FindNeighbor method, given a vector<int> node_indices
:
Create a pointer
Node *closest_node = nullptr
Declare a temporary Node variable
node
For each
node_index
innode_indices
Set
node
equal to theparent_model->SNodes()[node_index]
If the distance from
this
tonode
is nonzero, and thenode
has not been visited:If the
closest_node
equalsnullptr
, or the distance fromthis
tonode
is less than the distance fromthis
to*closest_node
:- Set
closest_node
to point to the address ofparent_model->SNodes()[node_index]
.
- Set
Finally, return the
closest_node
.
Note: there will be no new tests for this method. The method is private, and will be used as a helper function in a public method in the next exercise.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: react
- Opened files (when workspace is loaded): n/a
-
userCode:
export CXX=g++-7
export CXXFLAGS=-std=c++17
cmake_tests() {
/usr/local/bin/cmake -DTESTING="NodeDist" "$1"
}
export -f cmake_tests
Solution
Find Neighbor